﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.Linq;
using SS.WPF.Framework.Selection;
using SS.WPF.Framework.ViewModel;
using SS.WPF.Framework.ViewModel.Navigation;

namespace TestFramework.ViewModels
{
    [Export(typeof(INavigationCatalog))]
    public class DemoNavigationCatalog1ViewModel : NavigationBase<NavigationNode>
    {
        /// <summary>
        /// This property is used to import instances of classes implements interface INavigationNode
        /// </summary>
        [ImportMany(typeof(INavigationNode))]
        public Lazy<INavigationNode, IOrder>[] NavigationCalalog
        {
            set
            {
                var ordered = value.OrderBy(x => x.Metadata.Order);
                var children = new ObservableCollection<INavigationNode>(ordered.Select(x => x.Value));
                Children = new List<NavigationNode>(children.Cast<NavigationNode>());
            }
        }

        protected override void OnInitialize()
        {
            this.DisplayName = "Navigation Calalog 1";
            var navigationTreeNode = new NavigationNode
                                         {
                                             DisplayName = this.DisplayName
                                         };
            foreach (var efpSetting in Children)
            {
                efpSetting.Parent = navigationTreeNode;
            }
            base.OnInitialize();
        }

        public override void SelectionChanged(object sender, ISelection newSelection)
        {
            base.SelectionChanged(sender, newSelection);
            if (newSelection == null)
            {
                if (this.Children.First().IsSelected)
                {
                    this.Children.First().IsSelected = false;
                }

                this.Children.First().IsSelected = true;
            }
        }
    }
}
